package com.zhxy.service.impl;

import com.zhxy.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 向Redis中存储数据
     * @param key
     * @param value
     */
    @Override
    public void setValue(String key, Object value) {
        redisTemplate.opsForValue().set(key,value);
    }


    /**
     * 从Redis中获取数据
     * @param key
     * @return
     */
    @Override
    public Object getValue(String key) {
        return redisTemplate.opsForValue().get(key);
    }


    /**
     * 将数据从Redis中移除
     * @param key
     * @return
     */
    @Override
    public Boolean removeValue(String key) {
        return redisTemplate.delete(key);
    }

    /**
     * 设置过期时间
     * @param key
     */
    @Override
    public Boolean expire(String key, Long timeout, TimeUnit timeUnit) {
        return redisTemplate.expire(key,timeout,timeUnit);
    }
}
